package com.lute.controller;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.model.ClientProfile;
import com.lute.model.UserEntitlement;
import com.lute.utils.ServerErrorResponse;

@Controller
public class ClientAdminClientProfileUpdateServlet {
	
	@RequestMapping (value="/clientAdminClientProfileUpdate",method= RequestMethod.POST, headers = "Content-type=application/json")
	public @ResponseBody String accountingPeriodChecked(@RequestBody String clientProfileJSON, HttpServletRequest request) throws JSONException {
		String result = "";
		String clientId;
		/* example data: only deadlineToCloseAccountingPeriod and sendMailRemaider */
	    String deadlineToCloseAccountingPeriod;
		String sendMailRemainder;
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			jsonReq = new JSONObject(clientProfileJSON);
			clientId = jsonReq.getString("clientId");
			deadlineToCloseAccountingPeriod = jsonReq.getString("deadlineToCloseAccountingPeriod");
			sendMailRemainder = jsonReq.getString("sendMailRemainder");
		} catch(JSONException e) {
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		/* check if json data was not malformed */
		if( (jsonReq.isNull("deadlineToCloseAccountingPeriod")) && (jsonReq.isNull("sendMailRemaider"))) {
			jsonRes.put("result",ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		String role = (String)session.getAttribute("role");
	
		/* check  authorization */
		if(!(role.equals("clientAdmin"))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		int sessionUserId = (Integer)session.getAttribute("sessionUserId");
		int client_Id = Integer.parseInt(clientId);
		
		/* check if clientAdmin has privilege to client */
		ClientProfile clientProfile = UserEntitlement.checkClientAdminPrivilegeToClientProfile(sessionUserId, client_Id);

		if(clientProfile == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String[] parseDeadline = deadlineToCloseAccountingPeriod.split("-");
		int deadlineYear = Integer.parseInt(parseDeadline[0]);
		int deadlineMonth = Integer.parseInt(parseDeadline[1]);
		int deadlineDay = Integer.parseInt(parseDeadline[2]);
		GregorianCalendar deadline = new GregorianCalendar(deadlineYear,deadlineMonth, deadlineDay);
		
		String[] parseRemainder = sendMailRemainder.split("-");
		int remainderYear = Integer.parseInt(parseRemainder[0]);
		int remainderMonth = Integer.parseInt(parseRemainder[1]);
		int remainderDay = Integer.parseInt(parseRemainder[2]);
		GregorianCalendar remainder = new GregorianCalendar(remainderYear,remainderMonth, remainderDay);
		
		clientProfile.setDeadlineToCloseAccountingPeriod(deadline);
		clientProfile.setSendMailRemainder(remainder);
		
		ClientProfile.updateClientProfileInDB(clientProfile);
		
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		return result;
	}
}
